﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QLKetNoi;
using QLPhanQuyen.BO;

namespace QLPhanQuyen.GUI
{
    public partial class SuaNguoiDungForm : Office2007Form
    {
        List<string> NhomNguoiDung = new List<string>();
        NguoiDungBO NDBO = new NguoiDungBO();
        VaiTroBO VTBO = new VaiTroBO();

        private string _tenND;

        public SuaNguoiDungForm(string tenND)
        {
            _tenND = tenND;
            InitializeComponent();
            AttachKeyDownToControls(this);
            HienThiCmbVaiTro();
            DanhSachNhom();
            HienThiThongTinNhanVien();
        }

        public void HienThiThongTinNhanVien()
        {
            txtTenNguoiDung.Text = _tenND;
            txtTaiKhoan.Text = NDBO.LayTaiKhoanTheoTen(_tenND);
            txtMatKhau.Text = NDBO.LayMatKhauTheoTen(_tenND);
            txtXacNhan.Text = NDBO.LayMatKhauTheoTen(_tenND);
            txtDienGiai.Text = NDBO.LayDienGiaiTheoTen(_tenND);
            cmbVaiTro.SelectedText = VTBO.LayTenTheoMa(NDBO.LayMaTheoTen(_tenND));
            cmbNhom.SelectedText = NDBO.LayNhomTheoTen(_tenND);
            chkConQuanLy.Checked = NDBO.LayTrangThaiTheoTen(_tenND);
        }

        private void HienThiCmbVaiTro()
        {
            cmbVaiTro.DataSource = VTBO.DanhSachVaiTro();
            cmbVaiTro.ValueMember = "MaVaiTro";
            cmbVaiTro.DisplayMember = "TenVaiTro";
        }

        private void DanhSachNhom()
        {
            NhomNguoiDung.Add("Admin");
            NhomNguoiDung.Add("Nhân Viên");
            for (int i = 0; i < NhomNguoiDung.Count(); i++)
            {
                cmbNhom.Items.Add(NhomNguoiDung[i]);
            }
            cmbNhom.SelectedIndex = 0;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (KiemTraTextBox() == true)
            {
                if (
                    NDBO.SuaNguoiDung(
                    txtTenNguoiDung.Text,
                    txtTaiKhoan.Text,
                    txtMatKhau.Text,
                    cmbVaiTro.SelectedValue.ToString(),
                    NhomNguoiDung[cmbNhom.SelectedIndex],
                    chkConQuanLy.Checked,
                    txtDienGiai.Text))
                {
                    MessageBox.Show("Sửa người dùng thành công");
                }
                else
                {
                    MessageBox.Show("Không sửa được người dùng");
                }
            }
        }

        private void btnThemVaiTro_Click(object sender, EventArgs e)
        {
            ThemVaiTroForm frm = new ThemVaiTroForm();
            frm.Show();
        }

        void AttachKeyDownToControls(Control ccoll)
        {
            foreach (Control c in ccoll.Controls)
            {
                if (0 < c.Controls.Count)
                {
                    AttachKeyDownToControls(c);
                }
                else
                {
                    c.KeyDown += c_KeyDown;
                }
            }
        }

        void c_KeyDown(object sender, KeyEventArgs e)
        {
            Control c = sender as Control;
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    c.Parent.SelectNextControl(c, true, true, true, true);
                    e.Handled = true;
                    break;
            }
        }

        private void XoaLableLoi()
        {
            lblLoiNhanVien.Visible = false;
            lblLoiTaiKhoan.Visible = false;
            lblLoiMatKhau.Visible = false;
            lblLoiXacNhan.Visible = false;
        }
        /// <summary>
        /// Kiểm tra các textbox nhập liệu có bị null
        /// </summary>
        /// <returns></returns>
        private bool KiemTraTextBox()
        {
            bool kiemtra = true;
            //if (txtNhanVien.Text == "")
            //{
            //    lblLoiNhanVien.Visible = true;
            //    kiemtra = false;
            //}
            if (txtTaiKhoan.Text == "")
            {
                lblLoiTaiKhoan.Visible = true;
                kiemtra = false;
            }
            if (txtMatKhau.Text == "")
            {
                lblLoiMatKhau.Visible = true;
                kiemtra = false;
            }
            if (txtXacNhan.Text.Equals(txtXacNhan.Text) == false)
            {
                lblLoiXacNhan.Visible = true;
                kiemtra = false;
            }
            return kiemtra;
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
            if (System.Windows.Forms.Application.OpenForms[""] != null)
            {
                (System.Windows.Forms.Application.OpenForms[""] as PhanQuyenForm).Reload();
            }
        }
    }
}
